package com.zaofan.service;

import java.sql.SQLException;
import java.util.*;

import com.zaofan.bean.OrderPay;
import com.zaofan.bean.TopicReplyDTO;
import com.zaofan.dao.OrderPayDao;
import com.zaofan.exception.ServiceException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import static com.zaofan.utils.C3p0DataSourceUtils.getDataSource;

public class OrderPayService {
	private OrderPayDao orderPayDao = new OrderPayDao();

	public void delete(OrderPay orderPay) {
		orderPayDao.del(orderPay);
	}

	public void add(OrderPay orderPay) {
		orderPayDao.add(orderPay);
	}

	public void update(OrderPay orderPay) {
		orderPayDao.update(orderPay);
	}

	public List<OrderPay> findAll(Map<String, Object> params) {
		List<OrderPay> data = orderPayDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public List<OrderPay> findByProperty() {
		String sql = "select * from orderpay " +
				"where payState='申请退款'";

		System.err.println(sql);
		QueryRunner runner = new QueryRunner(getDataSource());
		try {
			List<OrderPay> list= runner.query(sql, new BeanListHandler<>(OrderPay.class));

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}
	public List<OrderPay> findByPropertys() {
		String sql = "select * from orderpay " +
				"where payState='已经退款'";

		System.err.println(sql);
		QueryRunner runner = new QueryRunner(getDataSource());
		try {
			List<OrderPay> list= runner.query(sql, new BeanListHandler<>(OrderPay.class));

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}
    public  OrderPay findByorderNo(String orderId){
        if(orderId!=null) {
            String sql = "Select * from orderpay where orderNo ="+ orderId;
			QueryRunner runner = new QueryRunner(getDataSource());
			try {
				List<OrderPay> list = runner.query(sql, new BeanListHandler<>(OrderPay.class));
				return list.get(0);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
     return null;
    }
	public Long count(Map<String, Object> params) {
		return orderPayDao.count(params);
	}

	public OrderPay findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return orderPayDao.findOne(params);
	}

}
